package com.sophialex.health.record.common.util;

import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.resource.StringConverter;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.preferences.ScopedPreferenceStore;

import com.sophialex.health.common.platform.Activator;
import com.sophialex.health.record.common.Messages;
import com.sophialex.health.system.frame.util.Assert;

/**
 * 本地参数类
 * 
 * @author ltt
 * 
 */
public class StoreUtil {
	public final static String COLUMN_WIDTH = "$COLUMN_WIDTH_"; // 列宽度 //$NON-NLS-1$
	public final static String COLUMN_TITLE = "$COLUMN_TITLE_";// 列名 //$NON-NLS-1$
	public final static String COLUMN_PROPERTIE = "$COLUMN_PROPERTIE_";// 列属性 //$NON-NLS-1$
	public final static String COLUMN_STYLE = "$COLUMN_STYLE_";// 列样式 //$NON-NLS-1$
	public final static String SEPARTER = "&"; //$NON-NLS-1$
	public final static String SEPARTER_fen = "_;_"; //$NON-NLS-1$
	public static IPreferenceStore store = Activator.getDefault() != null ? Activator
			.getDefault().getPreferenceStore()
			: new ScopedPreferenceStore(new InstanceScope(),
					Activator.PLUGIN_ID);

	public static final FontData[] defaultFontDate = Display.getCurrent()
			.getSystemFont().getFontData();
	/**
	 * 自动报警相关本地设置
	 */
	public static final String CANCEL_AUTO_ALARM = "$CANCEL_AUTO_ALARM"; //$NON-NLS-1$
	public static final String CANCEL_AUTO_ALARM_DEFAULT = "false"; //$NON-NLS-1$

	public static boolean cancelAlarm = false; // 是否取消自动报警

	public static final String AUTO_CLOSE_MINUTES = "$AUTO_CLOSE_MINUTES"; //$NON-NLS-1$
	public static final String AUTO_CLOSE_MINUTES_DEFAULT = "1"; //$NON-NLS-1$

	public static int autoCloseMinutes = 1; // 自动关闭时间（分钟）

	public static final String ALARM_SEPARATE_MINUTES = "$ALARM_SEPARATE_MINUTES"; //$NON-NLS-1$
	public static final String ALARM_SEPARATE_MINUTES_DEFAULT = "10"; //$NON-NLS-1$

	public static int alarmSeparateMinutes = 5; // 报警间隔时间（分钟）

	// 报警提醒天数
	public static final String ALARM_SEPARATE_TXTS = "$ALARM_SEPARATE_TXTS"; //$NON-NLS-1$
	public static final String ALARM_SEPARATE_TXTS_DEFAULT = "10"; //$NON-NLS-1$
	public static int alarmSeparateTxts = 10;
	// 报警设置-存量设置
	public static final String ARARM_SEPARARE_INVENTORY = "ARARM_SEPARARE_INVENTORY"; //$NON-NLS-1$
	public static final String ARARM_SEPARARE_INVENTORY_DEFAULT = "0"; //$NON-NLS-1$
	public static int alarmSeparateInventory = 0;
	
	// 门诊打印是否直接配药
	public static final String MZPY_PRINT_ISPY = "$MZPY_PRINT_ISPY"; //$NON-NLS-1$
	public static final String MZPY_PRINT_ISPY_DEFAULT = "1"; //$NON-NLS-1$
	public static int mzpyPrintIspy = 1;
	//
	/**
	 * 生成采购计划相关本地设置
	 */
	public static final String GENERATE_PURCHASE_HEAD_FONTDATA = "$GENERATE_PURCHASE_HEAD_FONTDATA"; // 表头字体 //$NON-NLS-1$
	public static final String GENERATE_PURCHASE_HEAD_FONTDATA_DEFAULT = StringConverter
			.asString(defaultFontDate);

	public static FontData[] genPurchaseHeadFd = defaultFontDate;

	public static final String GENERATE_PURCHASE_HEAD_HEIGHT = "$GENERATE_PURCHASE_HEAD_HEIGHT";// 表头高 //$NON-NLS-1$
	public static final String GENERATE_PURCHASE_HEAD_HEIGHT_DEFAULT = "26"; //$NON-NLS-1$

	public static int genPurchaseHeadHeight = 26;

	public static final String GENERATE_PURCHASE_ROW_FONTDATA = "$GENERATE_PURCHASE_ROW_FONTDATA";// 内容字体 //$NON-NLS-1$
	public static final String GENERATE_PURCHASE_ROW_FONTDATA_DEFAULT = StringConverter
			.asString(defaultFontDate);

	public static FontData[] genPurchaseRowFd = defaultFontDate;

	public static final String GENERATE_PURCHASE_ROW_HEIGHT = "$GENERATE_PURCHASE_ROW_HEIGHT";// 内容行高 //$NON-NLS-1$
	public static final String GENERATE_PURCHASE_ROW_HEIGHT_DEFAULT = "20"; //$NON-NLS-1$

	public static int genPurchaseRowHeight = 20;

	public static final String GENERATE_PURCHASE_PROGRAM = "$GENERATE_PURCHASE_PROGRAM"; // 显示列名 //$NON-NLS-1$
	// ，用purchaseSep隔开
	public static final String purchaseSep = "ztkjPurchaseSep"; //$NON-NLS-1$
	public static final String GENERATE_PURCHASE_PROGRAM_DEFAULT = Messages.StoreUtil_0
			+ purchaseSep + Messages.StoreUtil_27 + purchaseSep + Messages.StoreUtil_28 + purchaseSep + Messages.StoreUtil_29
			+ purchaseSep + Messages.StoreUtil_30 + purchaseSep + Messages.StoreUtil_31 + purchaseSep
			+ Messages.StoreUtil_32 + purchaseSep + Messages.StoreUtil_33 + purchaseSep + Messages.StoreUtil_34
			+ purchaseSep + Messages.StoreUtil_35 + purchaseSep + Messages.StoreUtil_36 + purchaseSep
			+ Messages.StoreUtil_37 + purchaseSep + Messages.StoreUtil_38;

	public static String genPurchaseProgram;

	public static String MZFYPDQUERYTIME = "$MZFYPDQUERYTIME"; //$NON-NLS-1$

	public static int mzyfpdQueryTime = 10; // 门诊药房排队情况查询刷新时间

	public final static String defaultKf = "$defaultKf"; // 默认库房 //$NON-NLS-1$
	public final static String sep = "$"; //$NON-NLS-1$

	public static void initLocalParam() throws Exception {

		if (!Assert.isNull(store.getString(GENERATE_PURCHASE_PROGRAM))) {
			genPurchaseProgram = store.getString(GENERATE_PURCHASE_PROGRAM);
		} else {
			genPurchaseProgram = GENERATE_PURCHASE_PROGRAM_DEFAULT;
		}

		if (!Assert.isNull(store.getString(GENERATE_PURCHASE_HEAD_HEIGHT))) {
			genPurchaseHeadHeight = (StringConverter.asInt(store
					.getString(GENERATE_PURCHASE_HEAD_HEIGHT)));
		} else {
			genPurchaseHeadHeight = (StringConverter
					.asInt(GENERATE_PURCHASE_HEAD_HEIGHT_DEFAULT));
		}
		if (!Assert.isNull(store.getString(GENERATE_PURCHASE_ROW_HEIGHT))) {
			genPurchaseRowHeight = (StringConverter.asInt(store
					.getString(GENERATE_PURCHASE_ROW_HEIGHT)));
		} else {
			genPurchaseRowHeight = (StringConverter
					.asInt(GENERATE_PURCHASE_ROW_HEIGHT_DEFAULT));
		}

		if (!Assert.isNull(store.getString(GENERATE_PURCHASE_HEAD_FONTDATA))) {
			genPurchaseHeadFd = (StringConverter.asFontDataArray(store
					.getString(GENERATE_PURCHASE_HEAD_FONTDATA)));
		} else {
			genPurchaseHeadFd = (StringConverter
					.asFontDataArray(GENERATE_PURCHASE_HEAD_FONTDATA_DEFAULT));
		}
		if (!Assert.isNull(store.getString(GENERATE_PURCHASE_ROW_FONTDATA))) {
			genPurchaseRowFd = (StringConverter.asFontDataArray(store
					.getString(GENERATE_PURCHASE_ROW_FONTDATA)));
		} else {
			genPurchaseRowFd = (StringConverter
					.asFontDataArray(GENERATE_PURCHASE_ROW_FONTDATA_DEFAULT));
		}
		if (!Assert.isNull(store.getString(CANCEL_AUTO_ALARM))) {
			cancelAlarm = (StringConverter.asBoolean(store
					.getString(CANCEL_AUTO_ALARM)));
		} else {
			cancelAlarm = (StringConverter.asBoolean(CANCEL_AUTO_ALARM_DEFAULT));
		}

		if (!Assert.isNull(store.getString(AUTO_CLOSE_MINUTES))) {
			autoCloseMinutes = (StringConverter.asInt(store
					.getString(AUTO_CLOSE_MINUTES)));
		} else {
			autoCloseMinutes = (StringConverter
					.asInt(AUTO_CLOSE_MINUTES_DEFAULT));
		}

		if (!Assert.isNull(store.getString(ALARM_SEPARATE_MINUTES))) {
			alarmSeparateMinutes = (StringConverter.asInt(store
					.getString(ALARM_SEPARATE_MINUTES)));

		} else {
			alarmSeparateMinutes = (StringConverter
					.asInt(ALARM_SEPARATE_MINUTES_DEFAULT));
		}

		if (!Assert.isNull(store.getString(ALARM_SEPARATE_TXTS))) {
			alarmSeparateTxts = (StringConverter.asInt(store
					.getString(ALARM_SEPARATE_TXTS)));

		} else {
			alarmSeparateTxts = (StringConverter
					.asInt(ALARM_SEPARATE_TXTS_DEFAULT));
		}
		
		if (!Assert.isNull(store.getString(MZPY_PRINT_ISPY))) {
			mzpyPrintIspy = (StringConverter.asInt(store
					.getString(MZPY_PRINT_ISPY)));

		} else {
			mzpyPrintIspy = (StringConverter
					.asInt(MZPY_PRINT_ISPY_DEFAULT));
		}
		
		// 报警设置-存量设置
		if (!Assert.isNull(store.getString(ARARM_SEPARARE_INVENTORY))) {
			alarmSeparateInventory = (StringConverter.asInt(store
					.getString(ARARM_SEPARARE_INVENTORY)));

		} else {
			alarmSeparateInventory = (StringConverter
					.asInt(ARARM_SEPARARE_INVENTORY_DEFAULT));
		}
	}

	/**
	 * 更新默认库房
	 * 
	 * @param fkfbh
	 * @param fkfjb
	 * @param fckh
	 */
	public static void updateDftKf(String fkfbh, Long fkfjb, Long fckh) {
		StringBuffer str = new StringBuffer(fkfbh).append(sep).append(fkfjb);
		if (fckh != null)
			str.append(fckh);
		store.setValue(defaultKf, str.toString());
	}

	public static String[] loadDftKf() {
		String str = store.getString(defaultKf);
		if (Assert.isNull(str))
			return null;
		String strs[] = str.split(sep);
		return strs;
	}

	/**
	 * 获取列宽
	 * 
	 * @param key
	 * @return
	 */
	public static String[] getColumnWidth(String key) {
		if (!Assert.isNull(store.getString(COLUMN_WIDTH + key))) {
			String str = store.getString(COLUMN_WIDTH + key);
			String[] width = str.trim().split(SEPARTER);
			return width;
		}
		return null;
	}

	/**
	 * 保存列宽
	 * 
	 * @param key
	 *            存储本地的key值。最好当前控件的所有类的全路径+该控件名作为key值以确保唯一
	 * @param value
	 *            以SEPARTER连接拼装的字体串。例：100&200&100即第一列宽为100,二列宽为200
	 */
	public static void save(String key, String value) {
		store.setValue(key, value);
	}
}
